package models;

public class VeiculoTree {
	private static VeiculoTNode root = null;
	private VeiculoTNode current = root;
	private Object[][] result = new Object[1][6];  
	private static int quantidade = 1;  
	private static int posicao = 0;	
		
	public VeiculoTNode find(int key_veiculo_id){
		current = root;
		while(current != null){
			if(key_veiculo_id < current.getVeiculoId())
				current = current.getLeftChild();
			if(key_veiculo_id > current.getVeiculoId())
				current = current.getRightChild();
			if(key_veiculo_id == current.getVeiculoId()){
				return current;
			}
		}
		return null;
	}
	
	public Object[][] list(int key_cliente_id){
		result = new Object[1][6];
		quantidade = 1;
		posicao = 0;
		return this.findList(root, key_cliente_id);
	}
	
	public Object[][] findList(VeiculoTNode localRoot, int key_cliente_id){
		if(localRoot != null){
			findList(localRoot.leftChild, key_cliente_id);
			generateArray(localRoot, key_cliente_id);
			findList(localRoot.rightChild, key_cliente_id);
		}
		return result;
	}
	
	public void generateArray(VeiculoTNode key_current, int key_cliente_id){
		if(key_cliente_id == key_current.getClienteId()){
			Object[][] temp = new Object[quantidade][6];
			for (int i = 0; i < result.length; i++)   
				temp[i] = result[i];
		    
			temp[posicao][0] = key_current.getVeiculoId();
			temp[posicao][1] = key_current.getClienteId();
			temp[posicao][2] = key_current.getMarcaId();
			temp[posicao][3] = key_current.getModeloId();
			temp[posicao][4] = key_current.getAno();
			temp[posicao][5] = key_current.getPlaca();
			result = temp;  
	        quantidade++;  
	        posicao++;
		}
	}
		
	public void insert(VeiculoTNode my_node){
		if(root == null){
			root = my_node;
		}
		else{
			VeiculoTNode current = root;
			VeiculoTNode parent;
			
			while(true){
				parent = current;
				if(my_node.getVeiculoId() < current.getVeiculoId()){
					current = current.getLeftChild();
					if(current == null){
						my_node.setVeiculoId();
						parent.setLeftChild(my_node);
						return;
					}
				}
				else{
					current = current.getRightChild();
					if(current == null){
						my_node.setVeiculoId();
						parent.setRightChild(my_node);
						return;
					}
				}
			}
		}
	}
	
	public static VeiculoTNode getRoot(){
		return root;
	}
}